package com.qxzn.module.mine.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ModifyPhoneCompleteActivity extends BaseActivity {

    @Override
    protected int initLayout() {
        return R.layout.activity_modify_phone_complete;
    }

    @Override
    protected void initView() {
        iv_back.setOnClickListener(view -> done());
    }


    private void done() {
        ActivityStack.finishAllWithoutActivity("com.qxzn.app.desktoprobot.MainActivity", "com.qxzn.module.mine.activity.UserInfoActivity");
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(R.string.modify_admin_title);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        showProgress();
        JSONObject param = new JSONObject();
        param.put("robot_id", SharePreferenceUtil.getDeviceId(this));
        param.put("username", getIntent().getStringExtra(VerificationCode.EXTRA_PHONE));
        HttpRequest.getService().updateRobotAdmin(HttpRequest.getDefaultBody(param)).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                hideProgress();
                BaseBean body = response.body();
                String prompt;
                if (body.getCode() == 200) {
                    EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.UPDATE_USER_INFO).build());
                    prompt = "已成功将机器人管理权转让给\n用户" + getIntent().getStringExtra(VerificationCode.EXTRA_PHONE);
                } else {
                    ToastUtil.showTextToast(ModifyPhoneCompleteActivity.this, body.getMsg());
                    prompt = body.getMsg();
                }
                ((TextView) findViewById(R.id.tv_complete_prompt)).setText(prompt);
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                hideProgress();
                ToastUtil.showTextToast(ModifyPhoneCompleteActivity.this, getString(R.string.network_error));
            }
        });
    }

    @OnClick(R2.id.tv_complete)
    public void onViewClicked() {
        done();
    }

    @Override
    public void onBackPressed() {
        done();
    }
}
